/****************************************************************
 *
 *  Project:
 *  Description: TODO
 *  Id:
 *  Date: 04/May/2010
 *  Copyright (c) Wellcome Trust  Sanger Institute 2010
 *
 *************************************************************** */
package uk.ac.sanger.www.WTSISearchService;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.rpc.ServiceException;

/**
 * 
 * @author mw8
 */
public class GetResultsFromSanger {

	public static void main(String args[]) {
		GetResultsFromSanger sanger = new GetResultsFromSanger();
		sanger.getSourcesWithTypesLikeKeywordsSearch("exon" );
	}

	public List<String> getSourcesForKeywordsSearch(String query) {

		List<String> ids = this.getIdsForDomain(query, "das_sources");
		System.out.println("ids.size="+ids.size());
		return ids;
		// now I want to generate a list of sources for these ids, probably want
		// a public requet to be able to do this

		// pass this list of sources back to the calling servlet which can then
		// write them out

	}

	public List getCoordinatesForKeyWords(String query) {

		List<String> ids = this.getIdsForDomain(query, "das_systems");
		return ids;
	}
	
	public List<String> getSourcesWithTypesLikeKeywordsSearch(String query) {

		List<String> ids = this.getIdsForDomain(query, "das_types");
		System.out.println("ids.size="+ids.size());
		return ids;
		// now I want to generate a list of sources for these ids, probably want
		// a public requet to be able to do this

		// pass this list of sources back to the calling servlet which can then
		// write them out

	}

	private List<String> getIdsForDomain(String keywords, String domain) {

		WTSISearchService_ServiceLocator serviceLocator = new WTSISearchService_ServiceLocator();
		WTSISearchService_PortType port = null;
		String[] ids = null;
		List<String> idList = new ArrayList<String>();

		try {
			port = serviceLocator.getWTSISearchServiceHttpPort();
			//port.getResults(domain, query, fields, start, size)
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			ids = port.getAllResultsIds(domain, keywords);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		for (String id : ids) {
			//System.out.println("id=" + id);
			idList.add(id);

		}
		return idList;
	}

}
